package com.ruge.core.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruge.core.exception.IError;
import com.ruge.core.tool.ServletTool;
import com.ruge.tool.base.BaseEnum;
import com.ruge.tool.json.JsonTool;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * description: Response
 * create time at 2022/6/6 11:35
 *
 * @author alice.ruge
 * @since 0.0.3
 */

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(description = "返回说明")
public class Response<T> implements Serializable {
    private static final long serialVersionUID = -6602365878131231511L;
    @ApiModelProperty(value = "返回状态", required = true)
    private Status status;
    @ApiModelProperty(value = "异常code")
    private String errorCode;
    @ApiModelProperty("异常信息")
    private String errorMessage;
    @ApiModelProperty("拓展信息")
    private String extMessage;
    @ApiModelProperty("请求路径")
    private String path;
    @ApiModelProperty("总数")
    private Long total;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    {
        HttpServletRequest request = ServletTool.getRequest();
        if (null != request) {
            this.path = request.getRequestURL().toString();
        }
    }

    public Response() {
        this.status = Status.SUCCEED;
    }

    public Response(IError error) {
        this.status = Status.SUCCEED;
        this.errorCode = error.getErrorCode();
        this.errorMessage = error.getErrorMessage();
        this.status = Status.FAILED;
    }

    public Response(Long total) {
        this.status = Status.SUCCEED;
        this.total = total;
    }

    public Response(T data) {
        this.status = Status.SUCCEED;
        this.data = data;
    }

    public Response(T data, Long total) {
        this.status = Status.SUCCEED;
        this.data = data;
        this.total = total;
    }

    public Response(Long total, T data) {
        this.status = Status.SUCCEED;
        this.data = data;
        this.total = total;
    }

    public static <T> Response<T> create() {
        return new Response<>();
    }

    public static <T> Response<T> create(IError error) {
        Response<T> response = new Response<>();
        response.errorCode = error.getErrorCode();
        response.errorMessage = error.getErrorMessage();
        response.status = Status.FAILED;
        return response;
    }

    public static <T> Response<T> create(IError error, String extMessage) {
        Response<T> response = new Response<>();
        response.errorCode = error.getErrorCode();
        response.errorMessage = error.getErrorMessage();
        response.status = Status.FAILED;
        response.extMessage = extMessage;
        return response;
    }

    public static <T> Response<T> create(T data) {
        return new Response<>(data);
    }

    public static <T> Response<T> create(Long total) {
        return new Response<>(total);
    }

    public static <T> Response<T> create(T data, Long total) {
        return new Response<>(data, total);
    }

    public static <T> Response<T> create(Long total, T data) {
        return new Response<>(data, total);
    }


    @Override
    public String toString() {

        return JsonTool.getObjToJson(this);
    }

    @Getter
    @AllArgsConstructor
    public enum Status implements BaseEnum {
        /**
         * 返回值状态枚举
         */
        SUCCEED("SUCCEED", "成功"),
        FAILED("FAILED", "失败"),
        ;
        private String code;
        private String desc;

        @Override
        public String toString() {
            return code + ":" + desc;
        }

    }
}
